package tool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class LCMCReader {
	public static HashMap<String, String> words = new HashMap<String, String>();
	public static HashSet<String> labels = new HashSet<String>();
	private final static String DIR = "LCMC";
	private final static String PARA_LABEL = "p";
	private final static String SENTENCE_LABEL = "s";
	private final static String WORD_LABEL = "w";

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		// LCMCReader lr = new LCMCReader();
		int total = resolveData(null);
		System.out.println("total = " + total);
		for (String l : labels) {
			System.out.println(l);
		}
		int totalLabel = labels.size();
		System.out.println("labels = " + totalLabel);

	}

	private static DocumentBuilderFactory factory;
	private static DocumentBuilder builder;
	private static Document document;

	public LCMCReader() throws ParserConfigurationException, SAXException,
			IOException {
		factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		builder = factory.newDocumentBuilder();
		File dir = new File(DIR);
		File[] file = dir.listFiles();
		for (File f : file) {
			System.out.println(f.getName());
		}
		document = builder.parse(file[0]);
		Element root = document.getDocumentElement();
		NodeList children = root.getChildNodes();
		System.out.println(root.getNodeName() + " " + root.getNodeValue());
		for (int i = 0; i < children.getLength(); ++i) {
			resolveNode(children.item(i), 1);
		}
	}

	public static int resolveData(String dir) throws Exception {
		int old = words.size();
		factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		builder = factory.newDocumentBuilder();
		if (dir == null)
			dir = DIR;
		File direction = new File(dir);
		File[] files = direction.listFiles();
		System.out.println("start to work");
		for (File f : files) {
			System.out.println("start to parse " + f.getName());
			document = builder.parse(f);
			Element root = document.getDocumentElement();
			resolveNode(root, 0);
			System.out.println("finished " + f.getName());
		}
		int now = words.size();
		return now - old;
	}

	private static void resolveNode(final Node root, final int indent) {
		NodeList children = root.getChildNodes();
		for (int i = 0; i < children.getLength(); ++i) {
			if (children.item(i).getNodeName().equals(PARA_LABEL)
					|| children.item(i).equals(SENTENCE_LABEL)) {
				resolveNode(children.item(i), indent + 1);
			} else if (children.item(i).getNodeName().equals(WORD_LABEL)) {
				if (children.item(i).getAttributes().getLength() == 1) {
					if (!words.containsKey(children.item(i).getTextContent())) {
						words.put(children.item(i).getTextContent(), children
								.item(i).getAttributes().item(0).getNodeValue());
						labels.add(children.item(i).getAttributes().item(0)
								.getNodeValue());
					}else{
						words.remove(children.item(i).getTextContent());
					}
				}
			} else {
				resolveNode(children.item(i), indent + 1);
			}
		}
	}

}
